﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Fz : MonoBehaviour, IPointerClickHandler
{

    //开关声音
    private AudioClip kaiguan;

    // Use this for initialization
    void Start () {
        //添加开关声音
        kaiguan = (AudioClip)Resources.Load("kaiguan", typeof(AudioClip));
	}
	
	// Update is called once per frame
	void Update () {
		
	}


    //开关点击
    public void OnPointerClick(PointerEventData eventData)
    {
        AudioSource.PlayClipAtPoint(kaiguan, this.transform.position);
      
        if (name == "sw1_a")
        {
            swOnclick(name,ref Init.sw1_status);
        }else if (name == "sw2_a")
        {
            swOnclick(name,ref Init.sw2_status);
        }
        lampStatus(Init.sw1_status, Init.sw2_status, Init.ebox1_sw1_status, Init.ebox1_sw2_status);
    }

    void swOnclick(string name,ref bool status)
    {
        if (status)
        {
            transform.Find(name + "0").transform.Rotate(-Vector3.left * 8);
            status = false;
        }
        else
        {
            status = true;
            transform.Find(name + "0").transform.Rotate(Vector3.left * 8);
        }
        lampStatus(Init.sw1_status, Init.sw2_status, Init.ebox1_sw1_status, Init.ebox1_sw2_status);
    }

    //配电箱电源开关点击
    void OnMouseDown()
    {

        if (this.name == "ebox1_sw1")
        {
            eboxSwClick(name,ref Init.ebox1_sw1_status);
        }
        else if (this.name == "ebox1_sw2")
        {
            eboxSwClick(name,ref Init.ebox1_sw2_status);
        }
        lampStatus(Init.sw1_status, Init.sw2_status, Init.ebox1_sw1_status, Init.ebox1_sw2_status);
    }


    void eboxSwClick(string name,ref bool status)
    {
        AudioSource.PlayClipAtPoint(kaiguan, this.transform.position);
        if (status)
        {
            status = false;
            transform.GetComponent<SpriteRenderer>().enabled = false;
        }
        else
        {
            status = true;
            transform.GetComponent<SpriteRenderer>().enabled = true;
        }
    }

    //灯的状态
    void lampStatus(bool sw1_status,bool sw2_status,bool ebox1_sw1_status, bool ebox1_sw2_status)
    {
        if (ebox1_sw1_status&&ebox1_sw2_status&&(sw1_status^sw2_status))
        {
            GameObject.Find("lamp1_a_2").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
            GameObject.Find("lamp1_a_2").GetComponent<Light>().enabled = true;
        }
        else
        {
            GameObject.Find("lamp1_a_2").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION");
            GameObject.Find("lamp1_a_2").GetComponent<Light>().enabled = false;
        }
    }


}
